# 1.一直循环
# while True:
#     src = input("输入::")
#     src = src.strip("吗?？")
#     print("机器人::",src)

# 2.随机生成15为含数字字符串
import string
import random
import copy
# string.ascii_letters:包含所有字母a-Z; string.digits:包含数字0~9的字符串
s = string.ascii_letters + string.digits
print(s)
# 返回指定长度的随机列表
num = random.sample(s, 15)
print(num)
# 将num合成字符串
num = "".join(num)
print(num)
"""
运行结果
    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
    ['7', '2', 'B', 'y', 'k', '9', 'n', '5', 'v', 'N', 'Z', 'U', 'j', 'D', 'm']
    72Byk9n5vNZUjDm
"""

#3.列表 list
l1 = [1, 2, 3]
print(l1.count(2))
l1.append(4)
print(l1)
# 将obj对象插入到列表中,插在index的前面对象
l1.insert(0, [3,4,5])
print(l1)
l1.insert(0, 3)
print(l1)
l1.remove(3)
print(l1)
l1.remove([3,4,5])
print(l1)
l1.pop()
print(l1)
print(len(l1))
#访问最后一个元素的方法
list2 = [1, 2, 3, 4]
a = list2[len(list2)-1]
b = list2[-1]
len = len(list2)
c = list2[(len-1): len]
d = list2[-1: ]
print(f"a={a},b={b},c={c},d={d}")
"""
运行结果
    1
    [1, 2, 3, 4]
    [[3, 4, 5], 1, 2, 3, 4]
    [3, [3, 4, 5], 1, 2, 3, 4]
    [[3, 4, 5], 1, 2, 3, 4]
    [1, 2, 3, 4]
    [1, 2, 3]
    3
    a=4,b=4,c=[4],d=[4]
"""

4.#元组，赋值后内存地址更改
a=(1,2,3)
print("id(a):",id(a))
a=(4,5,6)
print("id(a):",id(a))
a = 1, 2, 3, 4
print(a)
"""
运行结果
    id(a): 2344558628544
    id(a): 2344558665536
    (1, 2, 3, 4)
"""
# 元组与列表之间的区别
# (1)列表的元素值可以修改，元组的元素值无法修改
# (2)列表重新赋值内存地址不会变，元组的地址会变


5.#字典
# 无序
my = {'name':"stone", "age":18}
print(my['name'])
print(my['age'])
# print(my['test'])#Exception has occurred: KeyError 'test'
print(my.get('age'))
print(my.get('test'))
print(my.get('test',0))
# 区别：my['name']会报错，my.get('test')返回None

#方法
print(my.clear())
a={"cat":1, "dog":2}
print("cat" in a)
print("ca" in a)
for i in a.keys():
    print(i)
for i in a.values():
    print(i)
b = a.copy()
#浅拷贝内存地址不同
print(f"\ta->{a},b->{b}")
print(f"\tid(a)={id(a)}, id(b)={id(b)}")

print(a.pop("cat"))
print(a)
print(b)
print(b.pop("dog"))
print(b)
"""
运行结果
    stone
    18
    18
    None
    0
    None
    True
    False
    cat
    dog
    1
    2
            a->{'cat': 1, 'dog': 2},b->{'cat': 1, 'dog': 2}
            id(a)=2331934318784, id(b)=2331934407616
    1
    {'dog': 2}
    {'cat': 1, 'dog': 2}
    2
    {'cat': 1}
"""
# 6.集合
# 和字典有点像，没有value，无序，不重复
a = {1,2,3}
# a.add():向集合中添加元素
a.add(4)	#{1,2,3,4}
print(a)
# a.update():向集合中添加多个元素
a.update([5,6,7])	#{1,2,3,4,5,6,7}
print(a)
# a.remove():删除集合中的obj元素,不存在要报错
a.remove(4)	#{1,2,3,5,6,7}
print(a)
# a.pop():随机移除集合中的元素
a.pop()
print(a)

# a.discard():删除集合中的指定元素，若元素不存在，不报错
a.discard(10)
print("a.discard(10)->",a)
# a.clear():删除集合中的所有元素
a.clear()
print("a.clear()->",a)

#列表去重  
l = [1, 2,3,4,3,3,3,2]
a = set(l)	#去重
l = list(a) #变回来列表
print(l)

# 运算
a = {1,2,3,4}
b = {3,4,5,6}
set1 = a & b #{3,4}
set2 = a | b #{1,2,3,4,5,6}
set3 = a - b	#{1,2}
set4 = a^b	#{1,2,5,6}
print(f"set1={set1}, set2={set2}, set3={set3}, set{set4}")
"""
运行结果
    {1, 2, 3, 4}
    {1, 2, 3, 4, 5, 6, 7}
    {1, 2, 3, 5, 6, 7}
    {2, 3, 5, 6, 7}
    a.discard(10)-> {2, 3, 5, 6, 7}
    a.clear()-> set()
    [1, 2, 3, 4]
    set1={3, 4}, set2={1, 2, 3, 4, 5, 6}, set3={1, 2}, set{1, 2, 5, 6}
"""

# 7.浅拷贝与深拷贝
# 7.1 赋值
#非元组,对象为可变类型，值会跟着改变
l1 = [1,2,3,[4,5]]
l2 = l1 
l1.append(666)
print(f"\tl1->{l1}\n\tl2->{l2}")
print(f"\tid(l1)={id(l1)}, id(l2)={id(l2)}")
#元组，对象为不可变类型，值会跟着改变
t1 = (1,2,3)
t2 = t1
t1 = (4,5,6)
print(f"\tt1->{t1}, t2->{t2}")
print(f"\tid(t1)={id(t1)}, id(t2)={id(t2)}")

# 7.2 浅拷贝,深拷贝
l1 = [1,2,3,[4,5]]
l2 = copy.copy(l1)
#浅拷贝：若对象为可变对象，浅拷贝后创建新的独立空间，但是元素中包含可变对象时，改变其值会跟着改变
l3 = copy.deepcopy(l1) 
#深拷贝：可变对象中无论存在多少可变的元素对象，均独立不变
l1.append([121,666])
l1[3].append(9999)
print(f"\tl1->{l1} \n\tl2->{l2} \n\tl3->{l3}")
print(f"\tid(l1)={id(l1)} \n\tid(l2)={id(l2)} \n\tid(l3)={id(l3)}")
#元组：本身不可变，不受深浅拷贝的影响
t1 = (1,2,3)
t2 = copy.copy(t1)
t3 = copy.deepcopy(t1)
t1 = (4,5,6)
print(f"\tt1->{t1}, t2->{t2}, t3->{t3}")
print(f"\tid(t1)={id(t1)}, id(t2)={id(t2)}, id(t3)={id(t3)}")
''' 
运行结果
    id(l1)=1410139357632, id(l2)=1410139357632
    t1->(4, 5, 6), t2->(1, 2, 3)
    id(t1)=1410139097664, id(t2)=1410139063616
    l1->[1, 2, 3, [4, 5, 9999], [121, 666]]
    l2->[1, 2, 3, [4, 5, 9999]]
    l3->[1, 2, 3, [4, 5]]
    id(l1)=1410139360256
    id(l2)=1410139098688
    id(l3)=1410139357632
    t1->(4, 5, 6), t2->(1, 2, 3), t3->(1, 2, 3)
    id(t1)=1410139097664, id(t2)=1410139063616, id(t3)=1410139063616
'''